package day0415.day0415;

import java.util.concurrent.atomic.AtomicInteger;

public class LeetCode206 {
    /**
     * 反转链表
     */
    public static void main(String[] args) {

    }

    private ListNode reverseList(ListNode head) {
        // 遍历
//        ListNode pre = null;
//        ListNode next;
//        while (head != null) {
//            next = head.next;// 取出下一个
//            head.next = pre;// 交换/反转
//            pre = head;  // 继续下一个
//            head = next;
//        }
//        return pre;
        if (head == null || head.next == null) {
            return head;
        }
        ListNode next = head.next;
        ListNode newHead = reverseList(next);
        next.next = head;// 反转
        head.next = null;
        return newHead;
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}